package com.ly.houses.common;

import com.ly.houses.mapper.LoginMapper;
import com.ly.houses.model.User;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Component
public class LoginResolver implements HandlerMethodArgumentResolver {
//    @Resource
//    private LoginMapper loginMapper;
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.hasParameterAnnotation(Login.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
        Object object=request.getSession().getAttribute("user");
//        Object object=request.getAttribute("user");
        if(object==null){
            return null;
        }else{
            User user=(User) object;
            return user;
        }
    }
}
